package handlers

import (
	"encoding/json"
	"phys-mgr-sys/cache"
	"phys-mgr-sys/models"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func WarehouseAdd(c *gin.Context) {
	warehouse_code := c.PostForm("warehouse_code")
	warehouse_type := c.PostForm("warehouse_type")
	warehouse_address := c.PostForm("warehouse_address")
	data := models.Warehouse{
		WarehouseCode:    warehouse_code,
		WarehouseType:    warehouse_type,
		WarehouseAddress: warehouse_address,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func WarehouseOrderAdd(c *gin.Context) {
	warehouse_order_code := c.PostForm("warehouse_order_code")
	warehouse_order_name := c.PostForm("warehouse_order_name")
	warehouse_order_person := c.PostForm("warehouse_order_person")
	wid, _ := strconv.Atoi(c.PostForm("wid"))
	data := models.WarehouseOrder{
		WarehouseOrderCode:   warehouse_order_code,
		WarehouseOrderName:   warehouse_order_name,
		WarehouseOrderPerson: warehouse_order_person,
		Wid:                  wid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func WarehouseOrderList(c *gin.Context) {
	/*
		SELECT
		  warehouse_orders.id as WarehouseOrderId,
		  warehouse_orders.warehouse_order_code as WarehouseOrderCode,
		  warehouse_orders.warehouse_order_name as WarehouseOrderName,
		  warehouse_orders.warehouse_order_person as WarehouseOrderPerson,
		  warehouses.warehouse_code as WarehouseCode,
		  warehouses.warehouse_type AS WarehouseType,
		  warehouses.warehouse_address AS WarehouseAddress
		FROM
		  warehouse_orders
		  LEFT JOIN warehouses ON warehouse_orders.wid = warehouses.id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.WarehouseOrderList
	redis_key := "Warehouse_Order_List_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("warehouse_orders").
			Select(
				"warehouse_orders.id as WarehouseOrderId",
				"warehouse_orders.warehouse_order_code as WarehouseOrderCode",
				"warehouse_orders.warehouse_order_name as WarehouseOrderName",
				"warehouse_orders.warehouse_order_person as WarehouseOrderPerson",
				"warehouses.warehouse_code as WarehouseCode",
				"warehouses.warehouse_type AS WarehouseType",
				"warehouses.warehouse_address AS WarehouseAddress",
			).
			Joins("LEFT JOIN warehouses ON warehouse_orders.wid = warehouses.id").
			Offset(offset).Limit(size).Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.WarehouseOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
		"msg":   "ok",
	})
	return

}
func WarehouseOrderDetail(c *gin.Context) {
	/*
			SELECT
		  warehouse_orders.id AS WarehouseOrderId,
		  warehouse_orders.warehouse_order_code AS WarehouseOrderCode,
		  warehouse_orders.warehouse_order_name AS WarehouseOrderName,
		  warehouse_orders.warehouse_order_person AS WarehouseOrderPerson,
		  warehouses.warehouse_code AS WarehouseCode,
		  warehouses.warehouse_type AS WarehouseType,
		  warehouses.warehouse_address AS WarehouseAddress
		FROM
		  warehouse_orders
		  LEFT JOIN warehouses ON warehouse_orders.wid = warehouses.id
		  WHERE warehouse_orders.id=1
	*/
	id := c.Query("id")
	var list models.WarehouseOrderList
	redis_key := "Warehouse_Order_Detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("warehouse_orders").
			Select(
				"warehouse_orders.id as WarehouseOrderId",
				"warehouse_orders.warehouse_order_code as WarehouseOrderCode",
				"warehouse_orders.warehouse_order_name as WarehouseOrderName",
				"warehouse_orders.warehouse_order_person as WarehouseOrderPerson",
				"warehouses.warehouse_code as WarehouseCode",
				"warehouses.warehouse_type AS WarehouseType",
				"warehouses.warehouse_address AS WarehouseAddress",
			).
			Joins("LEFT JOIN warehouses ON warehouse_orders.wid = warehouses.id").
			Where("warehouse_orders.id=?", id).
			Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "ok",
	})
	return

}
